package com.supersit.greensproject.adapter;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.TextView;

import com.supersit.greensproject.R;

/**
 * Created by chen on 16/8/3.
 */
public class PriceGatherMaketSelectAutoTvAdapter extends CursorAdapter {
    /**
     * //     * 自动补全适配器
     * //     *
     * //     * @author Administrator
     * //
     */

    private int columnIndex;
    private SQLiteDatabase db;
    private String[] columns;

    public PriceGatherMaketSelectAutoTvAdapter(Context context, Cursor c, int col, String[] columns) {
        super(context, c);
        this.columnIndex = col;
        db = context.openOrCreateDatabase("greens.db", 0, null);
        this.columns = columns;
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        ((TextView) view).setText(cursor.getString(columnIndex));
    }

    @Override
    public String convertToString(Cursor cursor) {
        String data = cursor.getString(columnIndex);
        return data;
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        final LayoutInflater inflater = LayoutInflater.from(context);
        final View view = inflater.inflate(
                R.layout.autocompletetextview_item, null);
        TextView tv = (TextView) view
                .findViewById(R.id.tv_autocompletetextview_item);
        int id = cursor.getInt(cursor.getColumnIndex("id"));
        tv.setTextSize(20);
        tv.setText(cursor.getString(columnIndex));
        tv.setTag(id);
        return tv;
    }

    @Override
    public Cursor runQueryOnBackgroundThread(CharSequence constraint) {
        if (constraint != null) {
            String selection = "maketName like \'%"
                    + constraint.toString() + "%\' order by id limit 5";
            return db.query("maket", columns, selection, null, null,
                    null, null);
        } else {
            return null;
        }
    }


}
